package org.example.service;

import org.example.model.ExperimentStats;
import org.example.model.Student;
import org.example.model.Experiment;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.model.StudentStats;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

public class ExcelExporter {
    public static byte[] exportStudentStats(Map<Student, StudentStats> studentStatsMap) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("学生实验统计");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = createHeaderStyle(workbook);
            createCell(headerRow, 0, "学号", headerStyle);
            createCell(headerRow, 1, "姓名", headerStyle);
            createCell(headerRow, 2, "缺交次数", headerStyle);
            createCell(headerRow, 3, "缺交实验列表", headerStyle);

            // 填写学生统计信息
            int rowIndex = 1;
            for (Map.Entry<Student, StudentStats> entry : studentStatsMap.entrySet()) {
                Student student = entry.getKey();
                StudentStats stats = entry.getValue();

                Row row = sheet.createRow(rowIndex++);
                createCell(row, 0, String.valueOf(student.getId()), headerStyle);
                createCell(row, 1, student.getName(), headerStyle);
                createCell(row, 2, String.valueOf(stats.getMissingCount()), headerStyle);
                createCell(row, 3, stats.getMissingExperiments(), headerStyle);
            }

            // 自动调整列宽
            for (int i = 0; i < 4; i++) {
                sheet.autoSizeColumn(i);
            }

            // 将工作簿写入字节数组
            try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                workbook.write(outputStream);
                return outputStream.toByteArray();
            }
        }
    }

    public static byte[] exportExperimentStats(Map<Experiment, ExperimentStats> experimentStatsMap) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("实验提交统计");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = createHeaderStyle(workbook);
            createCell(headerRow, 0, "实验名称", headerStyle);
            createCell(headerRow, 1, "总人数", headerStyle);
            createCell(headerRow, 2, "缺交人数", headerStyle);
            createCell(headerRow, 3, "提交率 (%)", headerStyle);
            createCell(headerRow, 4, "缺交学生列表", headerStyle);

            // 填写实验统计信息
            int rowIndex = 1;
            for (Map.Entry<Experiment, ExperimentStats> entry : experimentStatsMap.entrySet()) {
                Experiment experiment = entry.getKey();
                ExperimentStats stats = entry.getValue();

                Row row = sheet.createRow(rowIndex++);
                createCell(row, 0, experiment.getName(), headerStyle);
                createCell(row, 1, String.valueOf(stats.getTotalStudents()), headerStyle);
                createCell(row, 2, String.valueOf(stats.getMissingCount()), headerStyle);
                createCell(row, 3, String.format("%.2f", stats.getSubmissionRate()), headerStyle);
                createCell(row, 4, String.join("、", stats.getMissingStudents().stream().map(Student::getName).toList()), headerStyle);
            }

            // 自动调整列宽
            for (int i = 0; i < 5; i++) {
                sheet.autoSizeColumn(i);
            }

            // 将工作簿写入字节数组
            try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                workbook.write(outputStream);
                return outputStream.toByteArray();
            }
        }
    }

    private static CellStyle createHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        font.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(font);
        style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    private static void createCell(Row row, int column, Object value, CellStyle headerStyle) {
        Cell cell = row.createCell(column);
        if (value instanceof Integer || value instanceof Long || value instanceof Double) {
            cell.setCellValue((Double) value);
        } else {
            cell.setCellValue(value.toString());
        }
        // 手动设置单元格样式
        CellStyle style = row.getSheet().getWorkbook().createCellStyle();
        // 设置样式
        cell.setCellStyle(style);
    }
}